package com.shop.controller;

import com.shop.annotation.RequiresPermissionsDesc;
import com.shop.pojo.DtsAdmin;
import com.shop.service.DtsAdminService;
import com.shop.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @ClassName AdminAdminController
 * @Author xiao宋
 * @Date 15:22  2022/10/14
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/admin")
public class AdminAdminController {
    @Autowired
    DtsAdminService adminService;

    /**
     * 删除用户 -->  得逻辑删除，不能真删除
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button ="删除管理员")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin){
        try {
            adminService.delete(admin);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    /**
     * 修改用户
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button ="修改管理员")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        log.info("修改用户 用户名-->{}",admin.getUsername());
        adminService.update(admin);
        log.info("修改成功");
        return ResponseUtil.ok(admin);
    }

    /**
     * 新增用户
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button ="添加管理员")
    @PostMapping("/create")
    public  Object creat(@RequestBody DtsAdmin admin){
        log.info("新增用户,用户名-->{}",admin.getUsername());
        adminService.insert(admin);
        return ResponseUtil.ok(admin);
    }


    /**
     * 显示用户列表的分页处
     * @param page          不传数据则默认第一页
     * @param limit         不传数据则默认每页20条数据
     * @param sort          不传数据则默认倒序
     * @param order         不传数据则默认通过新增的时间排序
     * @param username      不传数据则不传就是没有通过用户名查询
     * @return
     */
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button ="查询管理员列表")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue ="1" )Integer page,
            @RequestParam(value = "limit",required = false,defaultValue ="20" ) Integer limit,
            @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
            @RequestParam(value = "order",required = false,defaultValue ="desc" )String order,
            String username
    ){
        Map<String,Object>data=adminService.findByPage(page, limit, sort, order, username);
        return ResponseUtil.ok(data);
    }

}
